package com.bala.training;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class SerializationTest {

	public static void main(String[] args) {
   
   System.out.println("Hi there, Welcome!");

		//Creating a Dog instance d
		Dog d = new Dog(18, "Jerry");
		System.out.println("Before -> Dog's Name: " + d.name + " & wight: " + d.weight);

		//trying to serialize the Dog object d
		try {
			FileOutputStream fs = new FileOutputStream("TestSer.ser");
			ObjectOutputStream os = new ObjectOutputStream(fs);
			os.writeObject(d);
			os.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		//trying to deserialize the object which was saved in TestSer.ser and cast it to Dog object
		try {
			FileInputStream fis = new FileInputStream("TestSer.ser");
			ObjectInputStream ois = new ObjectInputStream(fis);
			d = (Dog) ois.readObject();
			ois.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		System.out.println("After -> Dog's Name: " + d.name + " & wight: " + d.weight);

	}

}

//Define a Dog class
class Dog extends Animal implements Serializable {
	String name = "Tiger";
	Dog() {
		name = "Tom";
	}
	Dog(int weight, String name) {
		this.weight = weight;
		this.name = name;
	}
}
//Define a Animal class
class Animal implements Serializable {
	transient int weight = 42;
}